⚡️ Speed up method TreeSitterAnalyzer._node_has_return by 14% in PR #1615 (codeflash/optimize-pr1561-2026-02-20T17.17.41)#1616
Closed
codeflash-ai[bot] wants to merge 1 commit intocodeflash/optimize-pr1561-2026-02-20T17.17.41from
Conversation
This optimization achieves a **14% runtime improvement** (395μs → 347μs) by eliminating repeated allocations and attribute lookups in a tree traversal algorithm.
## Key Optimizations
**1. Module-level frozenset for function types**
The original code recreated a tuple `("function_declaration", "function_expression", "arrow_function", "method_definition")` on every call. The optimized version moves this to a module-level `frozenset` (`_FUNC_LIKE_TYPES`), eliminating ~5.2% of the original function's overhead (5218ns per call). The frozenset also provides O(1) membership testing that's optimized at the C level.
**2. Localized `stack.extend` method**
By caching `stack.extend` as a local variable (`stack_extend = stack.extend`), the code avoids repeated attribute lookups on the list object. In tight loops with many iterations (the while loop executes 2521 times in the profiler), this removes thousands of attribute resolution steps. Line profiler shows this particularly benefits the two `stack_extend(reversed(children))` calls, which now execute ~10% faster (357305ns → 322655ns for the main case).
## Performance Characteristics
The optimization excels on workloads with:
- **Large/deep trees**: The `test_many_siblings_without_return_performance` (1000 nodes) shows **28.1% speedup** (86.1μs → 67.3μs)
- **Deep nesting**: The `test_deep_nesting_with_return_at_bottom` (1000 levels) shows **12.4% speedup** (198μs → 176μs)
- **Many function nodes**: The `test_many_functions_with_internal_returns_ignored_if_no_body` (500 functions) shows **10.1% speedup** (98.0μs → 89.0μs)
Small trees see modest overhead (1-8% slower) due to the additional local variable assignment, but this is outweighed by gains on realistic code analysis workloads where ASTs are typically large and deeply nested.
The optimizations preserve all original behavior—same traversal order, same return detection logic—making this a safe drop-in replacement that scales better with tree size.
| # reversed() returns an iterator; extend consumes it without creating an intermediate list | ||
| stack_extend(reversed(children)) | ||
| # Do not traverse other parts of the function node | ||
| # Do not traverse other parts of the function node |
Contributor
There was a problem hiding this comment.
Nit: This comment is duplicated on the line above. Remove the duplicate.
Suggested change
| # Do not traverse other parts of the function node |
Contributor
PR Review SummaryPrek Checks✅ All checks passed — ruff check and ruff format both pass with no issues. Mypy✅ No type errors found in Code ReviewThis PR makes a minor performance optimization to
Both are standard Python micro-optimizations that preserve the original traversal logic. Issues found:
No critical bugs, security issues, or breaking API changes found. Test Coverage
Last updated: 2026-02-20 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
⚡️ This pull request contains optimizations for PR #1615
If you approve this dependent PR, these changes will be merged into the original PR branch
codeflash/optimize-pr1561-2026-02-20T17.17.41.📄 14% (0.14x) speedup for
TreeSitterAnalyzer._node_has_returnincodeflash/languages/javascript/treesitter_utils.py⏱️ Runtime :
395 microseconds→347 microseconds(best of13runs)📝 Explanation and details
This optimization achieves a 14% runtime improvement (395μs → 347μs) by eliminating repeated allocations and attribute lookups in a tree traversal algorithm.
Key Optimizations
1. Module-level frozenset for function types
The original code recreated a tuple
("function_declaration", "function_expression", "arrow_function", "method_definition")on every call. The optimized version moves this to a module-levelfrozenset(_FUNC_LIKE_TYPES), eliminating ~5.2% of the original function's overhead (5218ns per call). The frozenset also provides O(1) membership testing that's optimized at the C level.2. Localized
stack.extendmethodBy caching
stack.extendas a local variable (stack_extend = stack.extend), the code avoids repeated attribute lookups on the list object. In tight loops with many iterations (the while loop executes 2521 times in the profiler), this removes thousands of attribute resolution steps. Line profiler shows this particularly benefits the twostack_extend(reversed(children))calls, which now execute ~10% faster (357305ns → 322655ns for the main case).Performance Characteristics
The optimization excels on workloads with:
test_many_siblings_without_return_performance(1000 nodes) shows 28.1% speedup (86.1μs → 67.3μs)test_deep_nesting_with_return_at_bottom(1000 levels) shows 12.4% speedup (198μs → 176μs)test_many_functions_with_internal_returns_ignored_if_no_body(500 functions) shows 10.1% speedup (98.0μs → 89.0μs)Small trees see modest overhead (1-8% slower) due to the additional local variable assignment, but this is outweighed by gains on realistic code analysis workloads where ASTs are typically large and deeply nested.
The optimizations preserve all original behavior—same traversal order, same return detection logic—making this a safe drop-in replacement that scales better with tree size.
✅ Correctness verification report:
🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-pr1615-2026-02-20T17.27.27and push.